package com.ewancle.service

import com.ewancle.entity.Fruit
import com.ewancle.tables.tables.references.FRUIT
import jakarta.inject.Singleton
import org.jooq.DSLContext
import org.jooq.impl.DSL
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Singleton
class UserService(private val dsl: DSLContext) {

    suspend fun getAllUsers(): List<Fruit> = withContext(Dispatchers.IO) {
        dsl.selectFrom(DSL.table("fruit"))
                .fetchInto(Fruit::class.java) // 将查询结果映射到 User 数据类
                .toList()

        /*dsl.selectFrom("fruit")
                .fetch()
                .map { record ->
                    Fruit(record.id, record.name)
                }*/
    }

    fun createUser(id: Long, name: String): Int {
        return dsl.insertInto(FRUIT)
                .set(FRUIT.ID, id)
                .set(FRUIT.NAME, name)
                .execute()
    }
}